package org.blogwu.learn.multithread.chapter3.demo4;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        ConditionThread thread1 = new ConditionThread();
        thread1.start();
        Thread.sleep(1000);
        ConditionThread.reentrantLock.lock();
        // 通知t1继续执行
        ConditionThread.condition.signal();
        System.out.println("主线程睡一下");
        Thread.sleep(5000);
        ConditionThread.reentrantLock.unlock();
    }
}

class ConditionThread extends Thread {
    static ReentrantLock reentrantLock = new ReentrantLock();
    static Condition condition = reentrantLock.newCondition();

    @Override
    public void run() {
        reentrantLock.lock();
        try {
            System.out.println("线程开始await");
            condition.await();
            System.out.println("线程执行完毕");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            reentrantLock.unlock();
        }
    }
}
